package com.aidong.carnival.controllers.mine;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.aidong.carnival.controllers.concerns.Failure;
import com.aidong.carnival.controllers.concerns.Result;
import com.aidong.carnival.controllers.concerns.Success;
import com.aidong.carnival.controllers.concerns.View.WithVotedList;
import com.aidong.carnival.entity.Talent;
import com.aidong.carnival.service.TalentService;
import com.fasterxml.jackson.annotation.JsonView;

@RestController
@RequestMapping("/mine")
public class TalentController {
    @Autowired
    TalentService talentService;

    @JsonView(WithVotedList.class)
    @RequestMapping("/apply")
    public Result show(
            @RequestParam("idongId") int idongId,
            @RequestParam(value = "carnivalId", defaultValue = "1") int carnivalId) {

        Talent talent = talentService.findByIdongId(carnivalId, idongId);
        if (talent == null) {
            Success result = new Success(new ModelMap().addAttribute("voted", talentService.selectVoted(carnivalId, idongId)));
            result.setCode(2);
            return result;
        } else {
            return new Success(talent);
        }

    }

    @JsonView(WithVotedList.class)
    @RequestMapping(value = "/apply", method = RequestMethod.POST)
    public Result apply(@RequestParam(value = "carnivalId", defaultValue = "1") int carnivalId,
                        @RequestParam(value = "idongId", required = true) int idongId,
                        @RequestParam(value = "type", required = true) String type, @RequestParam(value = "image[]") String[] image,
                        @RequestParam(value = "description", required = true) String description) {
        Talent talent = talentService.apply(carnivalId, idongId, type, image, description);
        if (talent != null) {
            return new Success(new ModelMap().addAttribute(talent));
        } else {
            return new Failure("您还未参加报名");
        }
    }
}
